
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * menuPagamento.java
 *
 * Created on 08/09/2011, 15:23:13
 */
/**
 *
 * @author Anthony
 */
public class menuPagamento extends javax.swing.JFrame {

    /** Creates new form menuPagamento */
    public menuPagamento() {
        initComponents();
        setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    DefaultTableModel modelo = new DefaultTableModel(null, new String[] {"RA", "Nome", "Multa"});
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        campoRA = new javax.swing.JTextField();
        campoNome = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(new java.awt.Color(102, 102, 255));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("RA");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, -1, -1));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Nome do Livro");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 20, -1, -1));

        campoRA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoRAActionPerformed(evt);
            }
        });
        jPanel1.add(campoRA, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 20, 90, -1));
        jPanel1.add(campoNome, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 20, 90, -1));

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton1.setText("Confirmar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 210, -1, -1));

        jButton2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 210, -1, -1));

        jButton3.setText("Buscar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 20, -1, -1));

        tabela.setModel(modelo);
        jScrollPane1.setViewportView(tabela);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 50, 440, 60));

        jRadioButton1.setBackground(new java.awt.Color(102, 102, 255));
        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jRadioButton1.setText("Dinheiro");
        jPanel1.add(jRadioButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 160, -1, -1));

        jRadioButton2.setBackground(new java.awt.Color(102, 102, 255));
        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jRadioButton2.setText("Cartão");
        jPanel1.add(jRadioButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 130, -1, -1));

        jRadioButton3.setBackground(new java.awt.Color(102, 102, 255));
        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jRadioButton3.setText("Cheque");
        jPanel1.add(jRadioButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 190, -1, -1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Forma de Pagamento:");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, -1, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(1, -3, 440, 240));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void campoRAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoRAActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoRAActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        try{
        Connection conexao = DriverManager.getConnection("jdbc:postgresql://localhost:5432/sisteca", "postgres", "postgres");
            String nome = campoNome.getText();
            String ra = campoRA.getText();
            String sql = "select ra,nome,dias from emprestimo where ra = ? and nome = ?";
            
            PreparedStatement query = conexao.prepareStatement(sql);
            query.setString(1, ra);
            query.setString(2, nome);
            ResultSet rs = query.executeQuery();
            double multa = 0;
            if(rs.next()){
                
            sql = "UPDATE emprestimo SET dias=(SELECT date(now()) - datadevo) WHERE ra=? AND nome=?";
            query = conexao.prepareStatement(sql);
            query.setString(1, ra);
            query.setString(2, nome);  
            query.execute();
            String diaAtraso = rs.getString("dias");
            double total = Double.parseDouble(diaAtraso);
            System.out.println(total); 
             if(total > 0){
                 multa = total;
                 
             }
             else{
                 multa = 0;
             }
             String mostrarMulta = String.valueOf(multa);
               
               tabela = new JTable(modelo); 
               String[] dados = new String[4];  
               dados[0] = rs.getString("ra");  
               dados[1] = rs.getString("nome");  
               dados[2] ="R$" + mostrarMulta;   
               modelo.addRow(dados);
               
           
             
            }
            else{
                if(campoRA.getText().equals("") || campoRA.getText().equals("")){
                    JOptionPane.showMessageDialog(null, "campo(s) não preenchido!");
                }
                else{
                    JOptionPane.showMessageDialog(null, "emprestimo não existe no sistema!");
                }
            }
            query.close();
            rs.close();
        } catch(Exception e){
            System.out.println("Deu erro...");
            e.printStackTrace();
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        try{
            Connection conexao = DriverManager.getConnection("jdbc:postgresql://localhost:5432/sisteca", "postgres", "postgres");
            String nome = campoNome.getText();
            String ra = campoRA.getText();
            String sql = "SELECT ra, nome FROM emprestimo WHERE ra=? AND nome=?";
            PreparedStatement query = conexao.prepareStatement(sql);
            query.setString(1, ra);
            query.setString(2, nome);
            ResultSet rs=query.executeQuery();
            if(rs.next()){
                query.close();
                rs.close();
                System.out.println("nome:" + campoRA.getText() + "ra: " + campoNome.getText());
                String sql2 = "delete from emprestimo where ra = ? and nome = ?";
                PreparedStatement query2 = conexao.prepareStatement(sql);
                query2.setString(1, ra);
                query2.setString(2, nome);
                query2.executeQuery();
                JOptionPane.showMessageDialog(null, "Pagamento realizado!");
            this.dispose();   
            }
            else{
                if(campoRA.getText().equals("") || campoRA.getText().equals("")){
                    JOptionPane.showMessageDialog(null, "campo(s) não preenchido!");
                }
                else{
                    JOptionPane.showMessageDialog(null, "emprestimo não existe no sistema!");
                }
            }
           
        }catch(Exception e){
             System.out.println("Deu erro...");
            e.printStackTrace();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new menuPagamento().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTextField campoNome;
    private javax.swing.JTextField campoRA;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabela;
    // End of variables declaration//GEN-END:variables
}
